import pandas as pd
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
from statsmodels.stats.multicomp import pairwise_tukeyhsd

df1 = None

def init_data():
    global df1

    df = pd.read_csv('data.csv')
    # print(df.head())

    df.columns=['专业必修课','专业选修课','公开课与通识教育','学校选修课','实践与其他']
    # print(df.head())

    df1=df.melt().dropna()
    df1.sort_values(by='variable',ascending=False,inplace=True)
    # print(df1)

def build_analysis_variance():
    a_model = ols('value~variable',df1).fit()
    anova_tb=anova_lm(a_model)
    print("======== print ANOVA table ========")
    print(anova_tb)
    print("======== print ANOVA table ========")

def Tukey_HSD():
    hsd = pairwise_tukeyhsd(df1['value'],df1['variable'])
    print("======== print Tukey HSD ========")
    print(hsd)
    print("======== print Tukey HSD ========")

    # df1.sort_values(by='variable',ascending=False,inplace=True)
    # print(df1['variable'])

if __name__ == '__main__':
    init_data()
    build_analysis_variance()
    Tukey_HSD()
